package cn.dxj1016.lesson02;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
//客户端
public class TcpClientDemo02 {
    public static void main(String[] args) throws Exception {
//        1.创建一个Socket连接
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
//        2.创建一个输出流
        OutputStream outputStream = socket.getOutputStream();

//        3.文件流 读取文件
        FileInputStream fileInputStream = new FileInputStream(new File("zly.jpg"));
//        4.写出文件
        byte[] bytes = new byte[1024];
        int len;
        while ((len = fileInputStream.read(bytes)) != -1) {
            outputStream.write(bytes,0,len);
        }
//通知服务器，我已经结束了
        socket.shutdownOutput();//我已经传输完了


//        确定服务器接收完毕，才能断开连接
        InputStream inputStream = socket.getInputStream();
//        String类型的数组
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bytes1 = new byte[1024];
        int len2;
        while ((len2 = inputStream.read(bytes1)) != -1) {
            byteArrayOutputStream.write(bytes1, 0, len2);
        }
        System.out.println(byteArrayOutputStream.toString());
//        5.关闭资源
        fileInputStream.close();
        inputStream.close();
        outputStream.close();
        socket.close();

    }
}
